package com.zsyh.jiujiu.home;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.zyh.toolslib.base.BaseActivity;
import com.example.zyh.toolslib.utils.EmptyUtil;
import com.example.zyh.toolslib.utils.SPUtils;
import com.example.zyh.toolslib.widget.TitleBar;
import com.google.gson.Gson;
import com.huantansheng.easyphotos.EasyPhotos;
import com.huantansheng.easyphotos.callback.SelectCallback;
import com.huantansheng.easyphotos.models.album.entity.Photo;
import com.lwb.retrofit.RetrofitTask;
import com.zsyh.jiujiu.Api;
import com.zsyh.jiujiu.R;
import com.zsyh.jiujiu.bean.SucessBean;
import com.zsyh.jiujiu.net.MyObserver;
import com.zsyh.jiujiu.utils.CompressPhotoUtils;
import com.zsyh.jiujiu.utils.GlideCircleTransform;
import com.zsyh.jiujiu.utils.GlideEngine;
import com.zsyh.jiujiu.utils.ToastUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;

/**
 * @ProjectName: livvehelp
 * @Package: com.zsyh.lsb.ui
 * @ClassName:
 * @Description: java类作用描述
 * @Author: zsyh
 * @CreateDate: 2020/9/7 11:59
 * @UpdateUser: liangwanbo
 * @UpdateDate: 2020/9/7 11:59
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class GoRenzhengActivity extends BaseActivity {

    @BindView(R.id.title_bar)
    TitleBar titleBar;
    @BindView(R.id.tvzheengfan)
    TextView tvzheengfan;
    @BindView(R.id.zhengmian)
    ImageView zhengmian;
    @BindView(R.id.bnt_zheng)
    TextView bntZheng;
    @BindView(R.id.fanmian)
    ImageView fanmian;
    @BindView(R.id.bnt_fan)
    TextView bntFan;
    @BindView(R.id.tvshouchi)
    TextView tvshouchi;
    @BindView(R.id.shouchi)
    ImageView shouchi;
    @BindView(R.id.bnt_shouchi)
    TextView bntShouchi;
    @BindView(R.id.quereen)
    TextView quereen;

    private String zhengimageurl,fanimageurl,shouimageurl;

    @Override
    protected int initLayout() {
        return R.layout.activitty_goren;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initEvent() {
        titleBar.setLeftLayoutClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }



    @OnClick({R.id.bnt_zheng, R.id.bnt_fan, R.id.bnt_shouchi, R.id.quereen})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.bnt_zheng:
                EasyPhotos.createAlbum(thisActivity, true, GlideEngine.getInstance())
                        .setFileProviderAuthority("com.zsyh.jiujiu.fileprovider")
                        .setCount(1)
                        .start(new SelectCallback() {
                            @Override
                            public void onResult(ArrayList<Photo> photos, boolean isOriginal) {
                                Glide.with(thisActivity)
                                        .load(photos.get(0).path)
                                        .into(zhengmian);
                                List<String> list=new ArrayList<>();
                                list.add(photos.get(0).path);
                                UpImage(list,"1");
                            }
                        });

                break;
            case R.id.bnt_fan:

                EasyPhotos.createAlbum(thisActivity, true, GlideEngine.getInstance())
                        .setFileProviderAuthority("com.zsyh.jiujiu.fileprovider")
                        .setCount(1)
                        .start(new SelectCallback() {
                            @Override
                            public void onResult(ArrayList<Photo> photos, boolean isOriginal) {
                                Glide.with(thisActivity)
                                        .load(photos.get(0).path)
                                        .into(fanmian);
                                List<String> list=new ArrayList<>();
                                list.add(photos.get(0).path);
                                UpImage(list,"0");


                            }
                        });

                break;
            case R.id.bnt_shouchi:

                EasyPhotos.createAlbum(thisActivity, true, GlideEngine.getInstance())
                        .setFileProviderAuthority("com.zsyh.jiujiu.fileprovider")
                        .setCount(1)
                        .start(new SelectCallback() {
                            @Override
                            public void onResult(ArrayList<Photo> photos, boolean isOriginal) {
                                Glide.with(thisActivity)
                                        .load(photos.get(0).path)
                                        .into(shouchi);
                                List<String> list=new ArrayList<>();
                                list.add(photos.get(0).path);
                                UpImage(list,"2");

                            }
                        });

                break;
            case R.id.quereen:

                if(EmptyUtil.isEmpty(zhengimageurl)){
                    ToastUtils.showShort(thisActivity,"请上传身份证正面");
                    return;
                }

                if(EmptyUtil.isEmpty(fanimageurl)){
                    ToastUtils.showShort(thisActivity,"请上传身份证反面");
                    return;
                }

                if(EmptyUtil.isEmpty(shouimageurl)){
                    ToastUtils.showShort(thisActivity,"请上传手持身份证");
                    return;
                }


                renzheng((String) SPUtils.get(thisActivity, "userid", ""),zhengimageurl,fanimageurl,shouimageurl);
                break;
        }
    }


    public void UpImage(List<String> list,String type) {
        new CompressPhotoUtils().CompressPhoto(this, list, new CompressPhotoUtils.CompressCallBack() {
            @Override
            public void success(List<String> list) {
                MultipartBody.Builder builder = new MultipartBody.Builder()
                        .setType(MultipartBody.FORM);//表单类型
                //多张图片
                for (int i = 0; i < list.size(); i++) {
                    File file = new File(list.get(i));//filePath 图片地址
                    RequestBody imageBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
                    builder.addFormDataPart("file", file.getName(), imageBody);//"imgfile"+i 后台接收图片流的参数名
                }
                List<MultipartBody.Part> parts = builder.build().parts();
                RetrofitTask.getInstance()
                        .createApi(Api.class)
                        .upload(parts)
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribeOn(Schedulers.io())
                        .subscribe(new Observer<ResponseBody>() {
                            @Override
                            public void onSubscribe(Disposable d) {

                            }

                            @Override
                            public void onNext(ResponseBody responseBody) {
                                try {
                                    String body=responseBody.string();
                                    Log.e("chengxintupian",body+"");
                                    JSONArray jsonArray=new JSONArray(body);
                                    String imageurl="";
                                    for(int i=0;i<jsonArray.length();i++){
                                        JSONObject jsonObject=jsonArray.getJSONObject(i);
                                        if(i==jsonArray.length()-1){
                                            imageurl=imageurl+jsonObject.getString("url");
                                        }else{
                                            imageurl=imageurl+jsonObject.getString("url")+",";
                                        }
                                    }

                                    if(type.equals("0")){
                                        fanimageurl=imageurl;
                                    }else if(type.equals("1")){
                                        zhengimageurl=imageurl;
                                    }else if(type.equals("2")){
                                        shouimageurl=imageurl;
                                    }



                                    Log.e("body",body+"");
                                } catch (IOException e) {
                                    e.printStackTrace();
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }

                            }

                            @Override
                            public void onError(Throwable e) {

                            }

                            @Override
                            public void onComplete() {

                            }
                        });

            }
        });
    }


    /**
     * 上传认证
     */
    public void renzheng(String userid,String zhen,String fan,String shou){
        RetrofitTask.getInstance()
                .createApi(Api.class)
                .userHonesty(userid,zhen,fan,shou)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new MyObserver(this,true) {
                    @Override
                    protected void onError(String errorMsg) {

                    }

                    @Override
                    protected void onSuccess(String JsonStr) {

                        Gson gson=new Gson();
                        SucessBean sucessBean=gson.fromJson(JsonStr,SucessBean.class);
                        ToastUtils.showShort(thisActivity,sucessBean.getMsg());
                        thisActivity.finish();
                    }
                });
    }

}
